package com.zoe.thread.th19_集合分片多线程;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.IdUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * @author zoe.Zhang
 * @date 2023/03/14
 * @description
 */
public class PartList {
    public static void main(String[] args) {
        ArrayList<String> objects = CollUtil.newArrayList();
        for (int i = 0; i < 100; i++) {
            objects.add(IdUtil.fastUUID());
        }
        int size = objects.size();
        System.out.println(size);
        ArrayList<CompletableFuture<String>> completableFutures = CollUtil.newArrayList();

        List<List<String>> splitList = CollUtil.split(objects, 5);

        for (List<String> strings : splitList) {
            CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
                ThreadUtil.sleep(1000); // 线程睡眠 1 秒
                return strings.toString();
            });
            completableFutures.add(stringCompletableFuture);
        }
        TimeInterval timer = DateUtil.timer();
        timer.start();
        List<String> collect = completableFutures.stream()
                                                 .map(CompletableFuture::join)
                                                 .collect(Collectors.toList());

        System.out.println(collect);
        System.out.println(timer.intervalSecond());
    }

}
